#include "chart.h"
#include <QtCharts>
#include <QtCharts/QAbstractAxis>
#include <QtCharts/QSplineSeries>
#include <QtCharts/QValueAxis>
#include <QtCharts/QDateTimeAxis>
#include <QtCore/QRandomGenerator>
#include <QtCore/QDebug>
#include <QtWidgets/QGesture>
#include <QtWidgets/QGraphicsScene>
#include <QtWidgets/QGraphicsView>
#include <QDateTime>

Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags,int a):
     QChart(QChart::ChartTypeCartesian, parent, wFlags),
//     m_series1(0),
     m_axisY(new QValueAxis()),
     m_series(0),
     m_axisX(new QValueAxis()),
//     m_axisY1(new QValueAxis()),
     m_step(0),
     m_x(0),
     m_y(1)
//     m_y1(2)
{
    //将图表和事件绑定
    grabGesture(Qt::PanGesture);
    grabGesture(Qt::PinchGesture);
    //选择图表的类型（1,2,3），并设置相应的格式
    flag = a;
    m_timer.setInterval(500);
    m_series = new QSplineSeries(this);
    QPen green(Qt::red);
    green.setWidth(2);
    m_series->setPen(green);
    m_series->append(m_x, m_y);
    addSeries(m_series);
    addAxis(m_axisX,Qt::AlignBottom);
    addAxis(m_axisY,Qt::AlignLeft);
    //将数据和坐标轴绑定并设置格式
    m_series->attachAxis(m_axisX);
    m_series->attachAxis(m_axisY);
    m_axisX->setTickCount(11);
    //设置时间坐标轴的上下限
    QDateTime begin_time =QDateTime::currentDateTime();
    m_axisX->setRange(-5,4);
    switch(flag){
        case 1:
               m_axisY->setTitleText("Flow/sccc");
               m_axisY->setRange(0, 10);
               m_timer.start();
               break;
        case 2:
               m_axisY->setTitleText("Pressure/kPa");
               m_axisY->setRange(0, 5);
               m_timer.start();
               break;
        case 3:
               m_axisY->setTitleText("Temp/℃");
               m_axisY->setRange(0, 15);
               m_timer.start();
               break;
        default: qDebug()<< "1error!";break;
     }
}

Chart::~Chart()
{

}

void Chart::handleTimeout(double res, int flag)
{
     qreal x = plotArea().width() / m_axisX->tickCount();
     qreal y = (m_axisX->max() - m_axisX->min()) / m_axisX->tickCount();
     m_y = res;
     m_x += y;
     m_series->append(m_x, m_y);
//     m_series1->append(m_x, m_y1);
     if(res > m_axisY->max() or res < m_axisY->min()){
         switch(flag){
                 case 1: m_axisY->setRange(m_y-0.01, m_y+0.01);break;
                 case 2: m_axisY->setRange(m_y-20, m_y+20);break;
                 case 3: m_axisY->setRange(m_y-0.5, m_y+0.5);break;
                 default: qDebug()<< "error!";break;
                 }
     }
     scroll(x, 0);
}

bool Chart::sceneEvent(QEvent *event)
{
    if (event->type() == QEvent::Gesture)
        return gestureEvent(static_cast<QGestureEvent *>(event));
    return QChart::event(event);
}

bool Chart::gestureEvent(QGestureEvent *event)
{
    if (QGesture *gesture = event->gesture(Qt::PanGesture)) {
        QPanGesture *pan = static_cast<QPanGesture *>(gesture);
        QChart::scroll(-(pan->delta().x()), pan->delta().y());
    }

    if (QGesture *gesture = event->gesture(Qt::PinchGesture)) {
        QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture);
        if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged)
            QChart::zoom(pinch->scaleFactor());
    }

    return true;
}

void Chart::handleAxis(double lower,double upper)
{
     m_axisY->setRange(lower, upper);
}
